<?php

namespace app\common\model;

use think\Model;

class GoodsCategory extends Model
{

    /**
     * 根据父id获取子分类
     * @param int $pid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCategoryByPid($pid = 0)
    {
        $data  = $this->where('pid',$pid)->paginate(20,false,['query'=>request()->param()]);;
        return $data;
    }

    /**
     * 根据父id获取全部子类
     * @param $pid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAllCategoryByPid($pid)
    {
        $data  = $this->where('pid',$pid)->order('id asc')->select();
        return $data;
    }

    /**
     * 根据主键id获取分类信息
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getCategoryById($id)
    {
        $data = $this->find($id);
        return $data;
    }

    /**
     * 判断分类名称是否重复
     * @param $category_name
     * @return bool
     */
    public function getCategoryByName($category_name)
    {
        $data = $this->where('category_name',$category_name)->select();
        if($data)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * 新增社区商品分类
     * @param $param
     * @return bool
     */
    public function add_goods_category($param)
    {
        $goods_category = new self();
        $goods_category->pid = $param['pid'];
        $goods_category->category_name = $param['category_name'];
        $goods_category->deep = $param['deep'];
        if($goods_category->save())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 编辑社区商品分类
     * @param $param
     * @return bool
     */
    public function edit_goods_category($id,$param)
    {
        $goods_category = new self();
        $goods_category = $goods_category->find($id);
        $goods_category->pid = $param['pid'];
        $goods_category->category_name = $param['category_name'];
        if($goods_category->save())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 删除某个专题商品
     * @param $id
     * @return bool
     */
    function del_one_goods_category($id)
    {
        $goods_category = new self();
        $goods_category = $goods_category->find($id);
        if ($goods_category->delete()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取商品的全部分类
     * @return false|static[]
     */
    public function get_all_category()
    {
        return $this->order('id')->select();
    }

    /**
     * 有序排序
     * @param $items
     * @return array
     */
    public function get_all_sort_List($items)
    {
        $tree = array();
        foreach($items as $item){
            if(isset($items[$item['pid']])){
                $items[$item['pid']]['son'][] = &$items[$item['id']];
            }else{
                $tree[] = &$items[$item['id']];
            }
        }
        return $tree;

    }
}
